package ui.fragment.user_management;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;
import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TitlePageIndicator;

import java.util.ArrayList;

import adapter.user_management.UserManagementFragmentPagerAdapter;
import controller.user_management.AllTabInUserManagementFragmentAsync;
import model.AllCompanyFeedItem;
import ui.activity.login.Login;

public class UserManagementFragment extends Fragment
        implements View.OnClickListener{

    /**
     * String section
     */

    /**
     * View section
     */
    private ViewPager mVpInUserManagementFragment;
    private PageIndicator mPiInUserManagementFragment;
    private UserManagementFragmentPagerAdapter userManagementFragmentPagerAdapter;
    public  static Context mContext;
    //The others Session
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            initialData();
            mHandler.removeCallbacks(this);
        }
    };

    private Handler mHandler = new Handler();

    public static Fragment newInstance() {

        UserManagementFragment fragment = new UserManagementFragment();
        return fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = getLayoutInflater(savedInstanceState).inflate(
                R.layout.fragment_user_management, container, false);
        mContext = getActivity();
        // Initial views
        setDataForUserManagementFragment();
        initialViews(v);
        /**
         * post delay to get all user number and number of users
         * before load user management fragment, because UserManagementFragmentPagerAdapter can not reset data
         */
        mHandler.postDelayed(mRunnable, 1000);
        return v;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //invisible ibtn invite user to company
        Enterprise.mIbtnInviteUserToCompany.setVisibility(View.GONE);
        Enterprise.mIbtnUpload.setVisibility(View.VISIBLE);
        Enterprise.mIbtnCreateGroup.setVisibility(View.GONE);

    }

    /**
     * Initialize methods
     */
    private void initialData() {
        userManagementFragmentPagerAdapter = new UserManagementFragmentPagerAdapter(getActivity(),
                getActivity().getSupportFragmentManager());
        mVpInUserManagementFragment.setAdapter(userManagementFragmentPagerAdapter);
        mPiInUserManagementFragment.setViewPager(mVpInUserManagementFragment);
        mPiInUserManagementFragment.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
//                switch(position){
//                    case 0:
//                        new AllTabInUserManagementFragment();
//                        break;
//                    case 1:
//                        new CompanyManagerTabInUserManagementFragment();
//                        break;
//                    case 2:
//                        new GroupManagerTabInUserManagementFragment();
//                        break;
//                    case 3:
//                        new GeneralUserTabInUserManagementFragment();
//                        break;
//                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initialViews(View v) {
        mVpInUserManagementFragment = (ViewPager) v.findViewById(R.id.vp_in_user_management_fragment);
        mPiInUserManagementFragment = (TitlePageIndicator) v.findViewById(R.id.title_page_indicator_in_user_management_fragment);
        //visible ibtn invite user to company
        Enterprise.mIbtnInviteUserToCompany.setVisibility(View.VISIBLE);
        Enterprise.mIbtnUpload.setVisibility(View.GONE);
        Enterprise.mIbtnCreateGroup.setVisibility(View.GONE);
    }

    /**
     * The others methods
     */

    public static void setDataForUserManagementFragment() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            new AllTabInUserManagementFragmentAsync(mContext).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    String.valueOf(Login.user.getUserInfo().getAccessToken()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyIdGroup()));
        else
            new AllTabInUserManagementFragmentAsync(mContext).execute(
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    String.valueOf(Login.user.getUserInfo().getAccessToken()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyIdGroup()));
    }
}
